package com.leyou.search.web;

import com.leyou.search.dto.SearchParamDTO;
import com.leyou.search.entity.Goods;
import com.leyou.search.service.SearchService;
import com.leyou.starter.elastic.dto.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;

@RestController
@RequestMapping("goods")
public class SearchController {

    @Autowired
    private SearchService searchService;

    /**
     *   索引库导入
     * @return  提示成功导入
     */
    @GetMapping("init")
    public ResponseEntity<String> initData(){
        //创建索引库
        searchService.createIndexAndMapping();
        //加载数据
        searchService.loadData();
        //返回成功
        return ResponseEntity.ok("索引库加载完毕");
    }

    /**
     *  搜索补全
     * @param key  查询信息
     * @return  返回自动补全信息
     */
    @GetMapping("suggestion")
    public Mono<List<String>> querySuggestion(@RequestParam("key") String key){
        return searchService.querySuggestion(key);
    }

    /**
     *   搜索业务
     * @param paramDTO  搜索请求参数
     * @return  搜索的分页信息
     */
    @PostMapping("list")
    public Mono<PageInfo<Goods>> serchGoods(@RequestBody SearchParamDTO paramDTO){
        return searchService.serchGoods(paramDTO);
    }

}
